package com.friend.base.controller.base;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.friend.base.service.base.MemberCircleService;
import com.friend.common.model.BaseController;
import com.friend.common.model.Result;
import com.friend.entity.entity.base.MemberCircle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author friend
 * @since 2019-10-23
 */
@Api(tags = {"会员圈子关系表"})
@RestController
@RequestMapping(value = "/memberCircle", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class MemberCircleController extends BaseController {

    @Autowired
    private MemberCircleService memberCircleService;

    @PostMapping("/joinCircle")
    @ApiOperation(value = "会员加入圈子", notes = "会员加入圈子", httpMethod = "POST")
    public Result joinCircle(@RequestBody MemberCircle memberCircle){
        try {
            boolean flag = memberCircleService.save(memberCircle);
            if (flag) {
                return this.createSuccessResult(flag);
            }else {
                return this.createFailedResult("加入圈子失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("加入圈子失败");
        }
    }

    @PostMapping("/exitCircle")
    @ApiOperation(value = "会员退出圈子", notes = "会员退出圈子", httpMethod = "POST")
    public Result exitCircle(@RequestBody MemberCircle memberCircle){
        try {
            MemberCircle relate = memberCircleService.getOne(new QueryWrapper<MemberCircle>().eq("memId", memberCircle.getMemId())
                    .eq("circleId", memberCircle.getCircleId()));
            if (BeanUtil.isEmpty(relate)) {
                return this.createFailedResult("退出圈子失败,该会员未加入该圈子");
            }else {
                boolean flag = relate.deleteById();
                if (flag) {
                    return this.createSuccessResult(flag);
                }else {
                    return this.createFailedResult("退出圈子失败");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("退出圈子失败");
        }
    }
}

